/*    This file is part of libmissive.
 *
 *    libmissive is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    libmissive is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with libmissive.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "box.h"
#include "seal.h"
#include "sign.h"
#include "transfer_crypt.h"

int
srt_sender_prep(Msg *msg, const Nonce nonce,
		const Box_skey client_skey, const Box_pkey client_pkey,
		const Box_pkey server_pkey, const Box_pkey recvr_pkey)
{
	Err err;
	Msg copy;

	if (msg_copy(&copy, msg, &err) < 0)
		return -1;

	if (boxinfo_box(&copy, nonce, client_skey,
			client_pkey, recvr_pkey) < 0 ||
	    unsealer_seal(&copy, recvr_pkey) < 0 ||
	    msg_seal(&copy, server_pkey) < 0) {
		msg_dispose(&copy);
		return -1;
	}

	msg_dispose(msg);
	*msg = copy;
	return 0;
}

int
srt_server_look(Msg *msg, const Box_skey server_skey,
		const Box_pkey server_pkey, Box_pkey recvr_pkey)
{
	Err err;
	Msg copy;

	if (msg_copy(&copy, msg, &err) < 0)
		return -1;

	if (msg_unseal(&copy, server_skey, server_pkey) < 0 ||
	    unsealer_remove(&copy, recvr_pkey) < 0) {
		msg_dispose(&copy);
		return -1;
	}

	msg_dispose(msg);
	*msg = copy;
	return 0;
}

int
srt_receiver_look(Msg *msg, Nonce nonce,
		  Box_pkey client_pkey, const Box_skey recvr_skey,
		  const Box_pkey recvr_pkey)
{
	Err err;
	Msg copy;

	if (msg_copy(&copy, msg, &err) < 0)
		return -1;

	if (msg_unseal(&copy, recvr_skey, recvr_pkey) < 0 ||
	    boxinfo_unbox(&copy, nonce, recvr_skey, client_pkey) < 0) {
		msg_dispose(&copy);
		return -1;
	}

	msg_dispose(msg);
	*msg = copy;
	return 0;
}

int
pub_sender_prep(Msg *msg, const Sign_skey client_skey,
		const Sign_pkey client_pkey, const Box_pkey server_pkey)
{
	Err err;
	Msg copy;

	if (msg_copy(&copy, msg, &err) < 0)
		return -1;

	if (msg_sign(&copy, client_skey) < 0 ||
	    signer_add(&copy, client_pkey) < 0 ||
	    msg_seal(&copy, server_pkey) < 0) {
		msg_dispose(&copy);
		return -1;
	}

	msg_dispose(msg);
	*msg = copy;
	return 0;
}

int
pub_server_look(Msg *msg, Sign_pkey client_pkey, const Box_skey server_skey,
		const Box_pkey server_pkey, Msg *recvr_msg)
{
	Err err;
	Msg tmp;
	Msg copy;

	if (!recvr_msg)
		recvr_msg = &tmp;

	if (msg_copy(recvr_msg, msg, &err) < 0)
		return -1;

	if (msg_unseal(recvr_msg, server_skey, server_pkey) < 0 ||
	    msg_copy(&copy, recvr_msg, &err) < 0) {
		msg_dispose(recvr_msg);
		return -1;
	}

	if (signer_open(&copy, client_pkey) < 0) {
		msg_dispose(&copy);
		msg_dispose(recvr_msg);
		return -1;
	}

	msg_dispose(msg);
	*msg = copy;
	return 0;
}

int
pub_receiver_look(Msg *msg, Sign_pkey client_pkey)
{
	return signer_open(msg, client_pkey);
}
